// my/wx.js
import {
  InvioceModel
} from '../../../models/invioce'
import {
  Format
} from '../../../utils/common'
const invioceModel = new InvioceModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 0,
    list: [],
    showWebview: false,
    webviewUrl: '',
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.findAlreadyInvoice(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.loadMyAccount()
  },
  findAlreadyInvoice(begin) {
    let vm = this
    invioceModel.findAlreadyInvoice(begin,8).then(res => {
      console.log(res)
      if (res) {
        if (res.code == '00') {
          for (let i = 0; i < res.data.length; i++) {
            switch (res.data[i].billingState) {
              case 0://
                res.data[i].billingTxt = '开票中'
                break;
              case 1://
                res.data[i].billingTxt = '开票成功'
                break;
              case 2://
                res.data[i].billingTxt = '开票失败'
                break;
              case 4://
                res.data[i].billingTxt = '未开票'
                break;
              case 5://
                res.data[i].billingTxt = '已开票'
                break;
                
            }
          }
          if (vm.data.page == 0) {
            vm.setData({
              list: res.data
            })
          } else {
              let list = this.data.list;
              for (let i = 0; i < res.data.length; i++) {
                list.push(res.data[i]);
              }
              vm.setData({
                list,
                show: false
              });            
          }
        } else {
          if (res.code) {
            wx.showToast({
              type: 'none',
              content: res.msg,
              duration: 1000,
            });
          } else {

          }
        }
      }
    })
  },
  scrollMytrip2() {
    try{
      this.setData({ show: true });
      this.data.page = this.data.page + 1
      this.findAlreadyInvoice(this.data.page)
    } catch(e) {
      this.setData({ show: false });
      console.log('scrollMytrip执行异常:', e);
    }
  },
  toDowload(e) {
    let item = e.currentTarget.dataset.item
    if (item.status == 0) {
      if (item.url) {
        // this.setData({
        //   showWebview: true,
        //   webviewUrl: item.url
        // })
        console.log(item.url)
        wx.navigateTo({
            url: '/pages/invioce/invioceDownloadBlock/invioceDownloadBlock?url=' + encodeURIComponent(item.url)
          })
      } else {
        wx.showToast({
          type: 'none',
          content: '发票还没有生成，请稍后下载',
          duration: 1000,
        });
      }
    } else {
      wx.showToast({
        type: 'none',
        content: '已红冲，不能下载',
        duration: 1000,
      });
    }
  },
  toDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/invioce/invioceDetail/invioceDetail?id=' + id
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})